package com.itheima.behaviour.listener;

import com.alibaba.fastjson.JSON;
import com.itheima.behaviour.dto.FollowBehaviorDto;
import com.itheima.behaviour.service.ApFollowBehaviorService;
import com.itheima.common.constants.BusinessConstants;
import com.itheima.common.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.listener.Topic;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class FollowBehaviorListener {

    /**
     * 保存用户行为,涉及的表有  ap_behavior_entry   行为实体表    ap_follow_behavior  APP关注行为表
     */
    @Autowired
    private ApFollowBehaviorService apFollowBehaviorService;
    @KafkaListener(topics = BusinessConstants.MqConstants.FOLLOW_BEHAVIOR_TOPIC)
    public void saveFollowBehavior(ConsumerRecord<String,String> msgRecord){
        log.info("用户关注行为 start: t={},p={},msg={}",msgRecord.topic(),msgRecord.partition(),msgRecord.value());
        //解析kafka消息，转为java对象
        FollowBehaviorDto followBehaviorDto = JSON.parseObject(msgRecord.value(), FollowBehaviorDto.class);
//        保存用户行为
        apFollowBehaviorService.saveFollowBehavior(followBehaviorDto);
        log.info("用户关注行为 end: t={},p={},msg={}",msgRecord.topic(),msgRecord.partition(),msgRecord.value());
    }
}
